{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 读写图像文件"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 每一个像素都有一个值，不同格式表示像素的方式不同。可以通过一个二维数组创建一个黑色的正方形图像："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 0 0]\n",
      " [0 0 0]\n",
      " [0 0 0]]\n"
     ]
    }
   ],
   "source": [
    "img = np.zeros((3,3),dtype=np.uint8)#每个像素都由一个8位整数表示，像素值范围是0-255。\n",
    "print(img)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 用cv2.cvtColor函数将图像转换成（Blue-Green-Red）BGR格式："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "img = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[0 0 0]\n",
      "  [0 0 0]\n",
      "  [0 0 0]]\n",
      "\n",
      " [[0 0 0]\n",
      "  [0 0 0]\n",
      "  [0 0 0]]\n",
      "\n",
      " [[0 0 0]\n",
      "  [0 0 0]\n",
      "  [0 0 0]]]\n"
     ]
    }
   ],
   "source": [
    "print(img)#每个像素由一个三元组表示，每个整型向量分别表示一个B、G、R通道。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 通过shape查看图像的结构，返回行和列。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 3, 3)\n"
     ]
    }
   ],
   "source": [
    "print(img.shape)#返回的3,3,3分别是行、列、通道数。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 读取一种格式的图像文件，保存为另一种格式。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "image = cv2.imread('one.jpg')#将png格式转换为jpg格式\n",
    "cv2.imwrite('one.png',image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[255 253 255]\n",
      "  [255 253 255]\n",
      "  [255 253 255]\n",
      "  ...\n",
      "  [255 253 255]\n",
      "  [255 253 255]\n",
      "  [255 253 255]]\n",
      "\n",
      " [[255 253 255]\n",
      "  [255 253 255]\n",
      "  [255 253 255]\n",
      "  ...\n",
      "  [255 253 255]\n",
      "  [255 253 255]\n",
      "  [255 253 255]]\n",
      "\n",
      " [[255 253 255]\n",
      "  [255 253 255]\n",
      "  [255 253 255]\n",
      "  ...\n",
      "  [255 253 255]\n",
      "  [255 253 255]\n",
      "  [255 253 255]]\n",
      "\n",
      " ...\n",
      "\n",
      " [[255 253 255]\n",
      "  [255 253 255]\n",
      "  [255 253 255]\n",
      "  ...\n",
      "  [255 253 255]\n",
      "  [255 253 255]\n",
      "  [255 253 255]]\n",
      "\n",
      " [[255 253 255]\n",
      "  [255 253 255]\n",
      "  [255 253 255]\n",
      "  ...\n",
      "  [255 253 255]\n",
      "  [255 253 255]\n",
      "  [255 253 255]]\n",
      "\n",
      " [[255 253 255]\n",
      "  [255 253 255]\n",
      "  [255 253 255]\n",
      "  ...\n",
      "  [255 253 255]\n",
      "  [255 253 255]\n",
      "  [255 253 255]]]\n",
      "(959, 959, 3)\n"
     ]
    }
   ],
   "source": [
    "print(image)\n",
    "print(image.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* BGR和RGB表示的色彩空间相同，，但是字节顺序相反。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* imread()函数参数\n",
    "\n",
    "> IMREAD_ANYCOLOR = 4\n",
    "\n",
    "> IMREAD_ANYDEPTH = 2\n",
    "\n",
    "> IMREAD_COLOR = 1\n",
    "\n",
    "> IMREAD_GRAYSCALE = 0\n",
    "\n",
    "> IMREAD_LOAD_GDAL = 8\n",
    "\n",
    "> IMREAD_UNCHANGED = -1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 例如:将png文件作为灰度图像，又保存为灰度的png图像："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grayImage = cv2.imread('two.png',cv2.IMREAD_GRAYSCALE)\n",
    "cv2.imwrite('two.png',grayImage)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[254 254 254 ... 254 254 254]\n",
      " [254 254 254 ... 254 254 254]\n",
      " [254 254 254 ... 254 254 254]\n",
      " ...\n",
      " [254 254 254 ... 254 254 254]\n",
      " [254 254 254 ... 254 254 254]\n",
      " [254 254 254 ... 254 254 254]]\n",
      "(959, 959)\n"
     ]
    }
   ],
   "source": [
    "print(grayImage)\n",
    "print(grayImage.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 图像与原始字节之间的转换"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 8位的灰度图是二维数组，24位的BGR图像是三维数组。如image[0,0]或image[0,0,0]。第一个值代表像素的y坐标或行，0表示顶部；第二个值代表像素的x坐标或列，0表示坐标；第三个值表示颜色通道。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 将含有随机字节的bytearray转换为灰度图像和BGR图像。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "randomByteArray = bytearray(os.urandom(120000))\n",
    "flatNumpyArray = np.array(randomByteArray)\n",
    "\n",
    "grayImage1 = flatNumpyArray.reshape(300,400)\n",
    "cv2.imwrite('randomGray.png',grayImage1)\n",
    "\n",
    "bgrImage = flatNumpyArray.reshape(100,400,3)\n",
    "cv2.imwrite('randomColor.png',bgrImage)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(300, 400)\n",
      "(100, 400, 3)\n"
     ]
    }
   ],
   "source": [
    "print(grayImage1.shape)\n",
    "print(bgrImage.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 将BGR图像在（0,0）处的像素转化为白像素。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "img = cv2.imread('image3.jpg')\n",
    "img[0,0] = [255,255,255]\n",
    "cv2.imshow('image',img)\n",
    "cv2.waitKey(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![title](img2.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 改变某一坐标的像素值，通过np.array提供的item()函数有3个参数，x,y以及x,y位置的数组索引，该函数返回索引位置的值。还可以通过itemset()函数可设置指定通道的值，有2个参数：一个三元组和索要设定的值。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 将坐标(150,120)的当前蓝色值(127)变为255："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "255\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "img = cv2.imread('image3.jpg')\n",
    "print(img.item(150,120,0))\n",
    "\n",
    "img.itemset((150,120,0),255)\n",
    "print(img.item(150,120,0))\n",
    "\n",
    "cv2.imshow('image',img)\n",
    "cv2.waitKey(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![title](img1.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 操作通道，将指定通道B/G/R的所有值置为0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "img = cv2.imread('one.jpg')\n",
    "img[:,:,1] = 0#将G值设为0,0表示B，1表示G，2表示R"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[255   0 255]\n",
      "  [255   0 255]\n",
      "  [255   0 255]\n",
      "  ...\n",
      "  [255   0 255]\n",
      "  [255   0 255]\n",
      "  [255   0 255]]\n",
      "\n",
      " [[255   0 255]\n",
      "  [255   0 255]\n",
      "  [255   0 255]\n",
      "  ...\n",
      "  [255   0 255]\n",
      "  [255   0 255]\n",
      "  [255   0 255]]\n",
      "\n",
      " [[255   0 255]\n",
      "  [255   0 255]\n",
      "  [255   0 255]\n",
      "  ...\n",
      "  [255   0 255]\n",
      "  [255   0 255]\n",
      "  [255   0 255]]\n",
      "\n",
      " ...\n",
      "\n",
      " [[255   0 255]\n",
      "  [255   0 255]\n",
      "  [255   0 255]\n",
      "  ...\n",
      "  [255   0 255]\n",
      "  [255   0 255]\n",
      "  [255   0 255]]\n",
      "\n",
      " [[255   0 255]\n",
      "  [255   0 255]\n",
      "  [255   0 255]\n",
      "  ...\n",
      "  [255   0 255]\n",
      "  [255   0 255]\n",
      "  [255   0 255]]\n",
      "\n",
      " [[255   0 255]\n",
      "  [255   0 255]\n",
      "  [255   0 255]\n",
      "  ...\n",
      "  [255   0 255]\n",
      "  [255   0 255]\n",
      "  [255   0 255]]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(img)\n",
    "cv2.imshow('image',img)\n",
    "cv2.waitKey(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 将图像的一部分拷贝到另一部分。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "img = cv2.imread('image4.jpg')\n",
    "my_roi = img[0:100,0:100]\n",
    "img[300:400,300:400] = my_roi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cv2.imshow('image',img)\n",
    "cv2.waitKey(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![title](img3.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 获取图像属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "图像维度为： (2560, 1440, 3)\n",
      "图像像素大小为： 2759043\n",
      "图像格式为： uint8\n"
     ]
    }
   ],
   "source": [
    "img = cv2.imread('image3.jpg')\n",
    "print('图像维度为：',img.shape)#返回图像宽度，高度，通道数\n",
    "print('图像像素大小为：',image.size)#图像像素的大小\n",
    "print('图像格式为：',img.dtype)#图像的数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
